<?php

   class CommonAction extends Action{
   
      public function _initialize(){
	  
	
	   if(session('adminislogin')!=2){
	     $this->redirect('Login/login');
	   }
	  }
	  
	  //分页类  table 表名，num 每页显示条数
	  public function listpage($table,$num,$order='',$where=''){
	    
		   $pagemodel=M($table);
		   import('ORG.Util.Page');
		   $count=$pagemodel->where($where)->count();
		   $page=new page($count,$num);
		   $limit=$page->firstRow.','.$page->listRows;
		   $re=$pagemodel->order($order)->limit($limit)->where($where)->select();
		   $this->re=$re;
		   $this->page=$page->show();
		 
	  }
	  
	  
	   public function page($table,$num,$order='',$where=''){
	    
		   $pagemodel=M($table);
		   import('ORG.Util.Page');
		   $count=$pagemodel->where($where)->count();
		   $page=new page($count,$num);
		   $limit=$page->firstRow.','.$page->listRows;
		   $re=$pagemodel->order($order)->limit($limit)->where($where)->select();
		   //$this->re=$re;
		   $this->page=$page->show();
		   return $re;
		 
	  }
	  //图片上传类
	  
	  public function upload($path){
	      
	        import('ORG.Net.UploadFile');
		    $config['savePath']       = "$path";
			$config['thumb']          = true;
		   
			$upload = new UploadFile($config);
            if($upload->upload()){
		    $info=$upload->getUploadFileInfo();
		    }else{
			$upload->getErrorMsg();
			}
			
			return $info;
	    
	  }
	  
	  //找出所有栏目 id 顶级栏目ID
	  
	  
	  
	    public function sel($cata,$mark='--',$pid=0,$level=0){
	  
	     $arr=array();
		 
		 foreach($cata as $v ){
		   
		    if($v['cid']==$pid){
			    $v['level']=$level+1;
			   $v['mark']=str_repeat($mark,$level);
			   $arr[]=$v;
			   $arr=array_merge($arr,$this->sel($cata,$mark,$v['id'],$level+1));
			}
		  }
		 return $arr;
	  }
	  
	  
	    public function sonid($cata,$pid){
  
      $arr=array();
	 // dump($cata);die;
	  foreach($cata as $v){
	    
	     if($v['topid']==$pid){
		   $arr[]=$v['id'];
		  
		   $arr=array_merge($arr,$this->sonid($cata,$v['id']));
		   
		 }
		 //dump($v);
	     
	  }
	 // dump($arr);
    return $arr;
  
  }
  
     public function uploadimg(){
	        
			 import('ORG.Net.UploadFile');
			$savepath='./images/'.date(Ymd).'/';
		    $config['savePath']       = $savepath;
		    $config['thumb']          = true;
		    $config['thumbPrefix']    = 'm_,s_';
		    $config['thumbMaxWidth']  = '200,50';
		    $config['thumbMaxHeight'] = '200,50';
			$config['maxSize'] = '500000000';
		    $upload = new UploadFile($config);
			//$upload->maxSize  = 50000000 ;
            if($upload->upload()){
		    $info=$upload->getUploadFileInfo();
			//dump($info);die;
			  echo json_encode(array(
			   "originalName"=>$info[0]['name'],
			   "name"=>$info[0]['savename'],
			   "url"=>'images/'.date(Ymd).'/'.$info[0]['savename'],
			   "size"=>$info[0]['size'],
			   "type"=>$info[0]['extension'],
			   "state"=>"SUCCESS"
			    )
			  );
			
		    }else{
			
			   echo json_encode(array(
			   "state"=>$upload->getErrorMsg()
			   
			   ));
			   
		   }
			
		   
		 }
   
	   
   
   }